using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AXBC = Microsoft.Dynamics.BusinessConnectorNet;
using AXMD = Microsoft.Dynamics.Framework.Metadata.Ax;
using Proxy = Microsoft.Dynamics.Framework.BusinessConnector.Proxy;

namespace DynamicsAXPS
{
    [System.Management.Automation.Cmdlet("Add", "AXRecord")]
    public class Add_AXRecord : System.Management.Automation.PSCmdlet
    {
        [System.Management.Automation.Parameter(Position = 0, Mandatory = true)]
        public string Table;

        [System.Management.Automation.Parameter(Position = 1, Mandatory = false)]
        public string[] Fields;

        [System.Management.Automation.Parameter(Position = 2, Mandatory = false)]
        public object[] Values;

        private static object[] emptyparams = new object[0];

        protected override void ProcessRecord()
        {
            var ax = new AXBC.Axapta();
            ax.Logon(null, null, null, null);
            using (var rec = ax.CreateAxaptaRecord(this.Table))
            {
                for (int i = 0; i < this.Fields.Length; i++)
                {
                    rec.set_Field(this.Fields[i], this.Values[i]);

                }
                rec.Insert();
            }

            ax.Logoff();
        }
    }
}
